<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Objekte &mdash; PowerShell fuer Systemintegratoren</title>
    
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="top" title="PowerShell fuer Systemintegratoren" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             accesskey="I">Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/PowerShell_icon_small.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Inhalt</a></h3>
  <ul>
<li><a class="reference internal" href="#">Objekte</a><ul>
<li><a class="reference internal" href="#aufgabe-1">Aufgabe 1:</a></li>
<li><a class="reference internal" href="#aufgabe-2">Aufgabe 2:</a></li>
<li><a class="reference internal" href="#aufgabe-3">Aufgabe 3:</a></li>
</ul>
</li>
</ul>

  <h3>Diese Seite</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/loesung_rst/Loesung_PowerShell_Objekte.txt"
           rel="nofollow">Quelltext anzeigen</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Schnellsuche</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Los" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Geben Sie Suchbegriffe oder einen Modul-, Klassen- oder Funktionsnamen ein.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="objekte">
<h1>Objekte<a class="headerlink" href="#objekte" title="Permalink zu dieser Überschrift">¶</a></h1>
<div class="section" id="aufgabe-1">
<h2>Aufgabe 1:<a class="headerlink" href="#aufgabe-1" title="Permalink zu dieser Überschrift">¶</a></h2>
<div class="sidebar">
<p class="first sidebar-title">Arbeitsblatt</p>
<ul class="last simple">
<li><a class="reference download internal" href="../_downloads/objekte_aufgabe11.pdf"><tt class="xref download docutils literal"><span class="pre">A1</span> <span class="pre">Unkommentiert</span></tt></a>.</li>
<li><a class="reference download internal" href="../_downloads/objekte_aufgabe1_comment1.pdf"><tt class="xref download docutils literal"><span class="pre">A1</span> <span class="pre">Kommentiert</span></tt></a>.</li>
</ul>
</div>
<div class="highlight-sh"><div class="highlight"><pre> <span class="nv">$strComputer</span> <span class="o">=</span> <span class="s2">&quot;.&quot;</span>

 <span class="c"># Neues leeres Objekt erstellen</span>
 <span class="nv">$Infos</span><span class="o">=</span> New-object -TypeName PSObject

 <span class="c"># Wert an das Objekt anfügen</span>
 Add-Member -InputObject <span class="nv">$Infos</span> -Name Manufacturer -Value 1 -MemberType NoteProperty
 <span class="c"># Wert an das Objekt anfügen</span>
 Add-Member -InputObject <span class="nv">$Infos</span> -Name Model -Value 2 -MemberType NoteProperty
 <span class="c"># Wert an das Objekt anfügen</span>
 Add-Member -InputObject <span class="nv">$Infos</span> -Name Memory -Value 3 -MemberType NoteProperty
 Add-Member -InputObject <span class="nv">$Infos</span> -Name BiosDescription -Value 3 -MemberType NoteProperty
 Add-Member -InputObject <span class="nv">$Infos</span> -Name SerialNumber -Value 3 -MemberType NoteProperty
 Add-Member -InputObject <span class="nv">$Infos</span> -Name OperatingSystem -Value 3 -MemberType NoteProperty

  Function SysInfo<span class="o">(</span><span class="nv">$Infos</span><span class="o">)</span>
  <span class="o">{</span>
      <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_ComputerSystem <span class="sb">`</span>
      -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> -ComputerName <span class="nv">$strComputer</span>

      foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
        <span class="nv">$Infos</span>.Manufacturer <span class="o">=</span>  <span class="nv">$objItem</span>.Manufacturer
        <span class="nv">$Infos</span>.Model <span class="o">=</span> <span class="nv">$objItem</span>.Model
       <span class="nv">$Infos</span>.Memory <span class="o">=</span>  <span class="nv">$objItem</span>.TotalPhysicalMemory
      <span class="o">}</span>
   <span class="o">}</span>

   Function BIOSInfo<span class="o">(</span><span class="nv">$Infos</span><span class="o">)</span>
   <span class="o">{</span>

      <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_BIOS -Namespace <span class="s2">&quot;root\CIMV2&quot;</span> <span class="sb">`</span>
      -computername <span class="nv">$strComputer</span>
      foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
              <span class="nv">$Infos</span>.BiosDescription <span class="o">=</span> <span class="nv">$objItem</span>.Description
              <span class="nv">$Infos</span>.SerialNumber <span class="o">=</span>  <span class="nv">$objItem</span>.SerialNumber
      <span class="o">}</span>
   <span class="o">}</span>


   Function OSInfo<span class="o">(</span><span class="nv">$Infos</span><span class="o">)</span>
   <span class="o">{</span>
      <span class="nv">$colItems</span> <span class="o">=</span> Get-WmiObject Win32_OperatingSystem -Namespace <span class="sb">`</span> <span class="s2">&quot;root\CIMV2&quot;</span> -Computername <span class="nv">$strComputer</span>

      foreach<span class="o">(</span><span class="nv">$objItem</span> in <span class="nv">$colItems</span><span class="o">)</span> <span class="o">{</span>
              <span class="nv">$Infos</span>.OperatingSystem <span class="o">=</span>  <span class="nv">$objItem</span>.Name
      <span class="o">}</span>
  <span class="o">}</span>



 <span class="c">#*=============================================================</span>
 <span class="c">#* SCRIPT BODY</span>
 <span class="c">#*=============================================================</span>
 <span class="c">#* Connect to computer</span>


 <span class="c">#* Call SysInfo Function</span>
 Write-Host <span class="s2">&quot;Sytem Information&quot;</span>
 SysInfo <span class="nv">$Infos</span>


<span class="c">#* Call BIOSinfo Function</span>
Write-Host <span class="s2">&quot;System BIOS Information&quot;</span>
BIOSInfo <span class="nv">$Infos</span>
Write-Host

<span class="c">#* Call OSInfo Function</span>
Write-Host <span class="s2">&quot;Operating System Information&quot;</span>
OSInfo <span class="nv">$Infos</span>
Write-Host


<span class="nv">$Infos</span> | Select-Object Name, Model

<span class="c"># CSV Export der Daten</span>
Export-Csv -InputObject <span class="nv">$Infos</span> -Path <span class="s2">&quot;C:\Temp\infos.csv&quot;</span>

 <span class="sb">`</span>
</pre></div>
</div>
</div>
<div class="section" id="aufgabe-2">
<h2>Aufgabe 2:<a class="headerlink" href="#aufgabe-2" title="Permalink zu dieser Überschrift">¶</a></h2>
<div class="sidebar">
<p class="first sidebar-title">Arbeitsblatt</p>
<ul class="last simple">
<li><a class="reference download internal" href="../_downloads/objekte_aufgabe21.pdf"><tt class="xref download docutils literal"><span class="pre">A2</span> <span class="pre">Unkommentiert</span></tt></a>.</li>
<li><a class="reference download internal" href="../_downloads/objekte_aufgabe2_comment1.pdf"><tt class="xref download docutils literal"><span class="pre">A2</span> <span class="pre">Kommentiert</span></tt></a>.</li>
</ul>
</div>
<div class="highlight-csharp"><pre>Measure-Command{
$site_urls = Import-CSV C:\temp\weekly_stats.csv

function bla
{

    foreach($url in $site_urls)
    {
        $obj_avg = New-Object -TypeName psobject
        Add-Member -InputObject $obj_avg -MemberType NoteProperty -Name "site_url" -Value $url.site_url
        Add-Member -InputObject $obj_avg -MemberType NoteProperty -Name "url_avg" `
            -Value ([int](([int]$url.monday + [int]$url.tuesday + [int]$url.wednesday + [int]$url.thursday + [int]$url.friday + [int]$url.saturday + [int]$url.sunday)/7))
        $obj_avg
    }

}

bla | Sort-Object url_avg

}




Measure-Command {
$site_urls2 = Import-CSV C:\temp\weekly_stats.csv

$site_urls2 | Select @{name="site_url";expression={$_.site_url}}, `
@{name="avg";expression={([int](([int]$_.monday + [int]$_.tuesday + [int]$_.wednesday + [int]$_.thursday + [int]$_.friday + [int]$_.saturday + [int]$_.sunday)/7))}} | `
Sort-Object avg
}







add-type @"
using System;
public class siteurl{

        public string url = "";
        public double durchschnitt = 0;

        public void seturl(string _url)
        {
            this.url = _url;
        }

        public void avg(int mon, int tue, int wed, int thu, int fri, int sat, int sun)
        {
                this.durchschnitt =  ((monday+tuesday+wednesday+ thursday + friday+ saturday + sunday)/7);
        }
}
"@

Measure-Command {
function bla2
{

$site_urls = Import-CSV C:\temp\weekly_stats.csv

    foreach($url in $site_urls)
    {
        $net_avg = New-Object -typename siteurl
        $net_avg.seturl($url.site_url)
        $net_avg.avg($url.monday,$url.tuesday,$url.wednesday,$url.thursday,$url.friday,$url.saturday,$url.sunday)
        $net_avg
    }

}

bla2 |select url, durchschnitt | Sort-Object durchschnitt
 `</pre>
</div>
</div>
<div class="section" id="aufgabe-3">
<h2>Aufgabe 3:<a class="headerlink" href="#aufgabe-3" title="Permalink zu dieser Überschrift">¶</a></h2>
<p>Gegeben ist folgendes PS-Skript.</p>
<div class="highlight-sh"><div class="highlight"><pre>Function Get-NetworkConfiguration
<span class="o">{</span>
    param <span class="o">(</span>
        <span class="o">[</span>parameter<span class="o">(</span>
            <span class="nv">ValueFromPipeline</span><span class="o">=</span><span class="nv">$true</span>,
            <span class="nv">ValueFromPipelineByPropertyName</span><span class="o">=</span><span class="nv">$true</span>,
            <span class="nv">Position</span><span class="o">=</span>0<span class="o">)]</span>
        <span class="o">[</span>Alias<span class="o">(</span><span class="s1">&#39;__ServerName&#39;</span>, <span class="s1">&#39;Server&#39;</span>, <span class="s1">&#39;Computer&#39;</span>, <span class="s1">&#39;Name&#39;</span><span class="o">)]</span>
        <span class="o">[</span>string<span class="o">[]]</span>
        <span class="nv">$ComputerName</span> <span class="o">=</span> <span class="nv">$env</span>:COMPUTERNAME,
        <span class="o">[</span>parameter<span class="o">(</span><span class="nv">Position</span><span class="o">=</span>1<span class="o">)]</span>
        <span class="o">[</span>System.Management.Automation.PSCredential<span class="o">]</span>
        <span class="nv">$Credential</span>
    <span class="o">)</span>
    process
    <span class="o">{</span>
        <span class="nv">$WMIParameters</span> <span class="o">=</span> @<span class="o">{</span>
</pre></div>
</div>
<ul>
<li><p class="first">Wählen Sie statt der Write-Host-Ausgabe einen objektorientieten Ansatz</p>
<p>Der foreach-teil muss wie folgt geändert werden:</p>
<div class="highlight-sh"><div class="highlight"><pre>foreach <span class="o">(</span><span class="nv">$adapter</span> in <span class="o">(</span>Get-WmiObject @WMIParameters<span class="o">))</span>
<span class="o">{</span>
    <span class="nv">$AdapterProperties</span> <span class="o">=</span> @<span class="o">{</span>
        <span class="nv">Server</span> <span class="o">=</span> <span class="nv">$adapter</span>.DNSHostName
        <span class="nv">Adapter</span> <span class="o">=</span>  <span class="nv">$adapter</span>.Description
        <span class="nv">IPAddress</span> <span class="o">=</span> <span class="nv">$adapter</span>.IpAddress
        <span class="nv">SubnetMask</span> <span class="o">=</span> <span class="nv">$adapter</span>.IPSubnet
        <span class="nv">DefaultGateway</span> <span class="o">=</span> <span class="nv">$adapter</span>.DefaultIPGateway
        <span class="nv">DNSServers</span> <span class="o">=</span> <span class="nv">$adapter</span>.DNSServerSearchOrder
        <span class="nv">DNSDomain</span> <span class="o">=</span> <span class="nv">$adapter</span>.DNSDomain
    <span class="o">}</span>

    New-Object PSObject -Property <span class="nv">$AdapterProperties</span>
<span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Sie wollen nicht alle Informationen ausgeben, sondern beispielsweise nur die IP-Adresse und die Subnet-Maske</p>
<blockquote>
<div><div class="highlight-sh"><div class="highlight"><pre>Get-NetworkConfiguration ‘Server1’, ‘Server2’, ‘Server3’ | Format-Table Server, Adapter, SubnetMask, DefaultGateway –auto –wrap
</pre></div>
</div>
</div></blockquote>
</li>
<li><p class="first">Können Sie mit diesem Skript mehrere Rechner abfragen ?</p>
<p>ja</p>
</li>
<li><p class="first">Welche Rechner ihres Netzes benutzen den gleichen DNS-Server mit der
IP-Adresse <strong>*.*</strong>.***.***. Eine Liste der Computer erhalten Sie mit Hilfe des CommandLets get-adcomputer</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-ADComputer –filter * | Get-NetworkConfiguration | Where-Object <span class="o">{</span><span class="nv">$_</span>.DNSServers –contains <span class="nv">$IPofMyTroublesomeDNSServer</span><span class="o">}</span>
</pre></div>
</div>
</li>
<li><p class="first">Wie viele Rechner benutzen den gleichen Default-Gateway</p>
<div class="highlight-sh"><div class="highlight"><pre>Get-ADComputer -filter * | Get-NetworkConfiguration | Group-Object DefaultGateway -NoElement
</pre></div>
</div>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="Allgemeiner Index"
             >Index</a></li>
        <li><a href="../index.html">PS4SYS</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010, STE.
      Mit <a href="http://sphinx-doc.org/">Sphinx</a> 1.2b1 erstellt.
    </div>
  </body>
</html>